package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.*
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 作者：SimGa
 * 日期：2020/12/29
 * 时间：15:41
 * 包名：com.vitabuybuy.tv.network
 */
object GroupBuyRepository {

    fun getGroupBuyList(
        map: HashMap<String, Any>,
        pageNumber: Int
    ): IAwait<MutableList<GroupBuyListDto>> {
        map["pageable"] = mapOf(
            Pair("page", pageNumber),
            Pair("size", 10),
            Pair("desc", "id")
        )
        return RxHttp.postJson(NetUrl.api_find_groupbuy_list)
            .addAll(map).toResponse()
    }

    fun getGroupBuyDetail(id: Int?): IAwait<GroupBuyDetailDto> {
        return RxHttp.postJson(NetUrl.api_find_groupbuy_detail)
            .add("id", id).toResponse()
    }

    fun getGroupBuyMember(goodId: Int): IAwait<MutableList<GroupBuyMemberDto>> {
        return RxHttp.postJson(NetUrl.api_group_buy_member)
            .add("product", mapOf(Pair("id", goodId)))
            .add(
                "pageable", mapOf(
                    Pair("page", 1),
                    Pair("size", 50),
                    Pair("desc", "create_time")
                )
            )
            .toResponse()
    }


    fun getMyGroupBuyOrderList(
        groupType: String?,
        map: HashMap<String, Any>,
        pageNumber: Int
    ): IAwait<MutableList<GroupBuyOrderListDto>> {
        map["pageable"] = mapOf(
            Pair("page", pageNumber),
            Pair("size", 10),
            Pair("desc", "id")
        )
        return RxHttp.postJson(
            if (groupType != null)
                NetUrl.api_group_buy_order_list
            else NetUrl.api_my_group_limit
        ).addAll(map).toResponse()
    }

    fun getMyGroupBuyDetail(
        id: Int?,
        type: Int?
    ): IAwait<MyGroupBuyDetailDto> {
        return RxHttp.postJson(
            if (type == 1)
                NetUrl.api_my_group__cluster_detail
            else NetUrl.api_my_group__limited_detail
        ).add("id", id).toResponse()
    }

    fun collectOrcancel(id: Int, isFavorites: Boolean): IAwait<String> {
        return if (!isFavorites) {
            RxHttp.postJson(NetUrl.api_collect_good_or_store)
                .add("favorites",  mapOf(Pair("product", mapOf(Pair("id", id))))).toResponse()
        } else {
            RxHttp.postJson(NetUrl.api_cancel_collect_good_or_store)
                .add("product", mapOf(Pair("id", id))).toResponse()
        }
    }
}